
**first import the 2016 dataset 
import delimited "datasource/MPDataset_MPDS2016b.csv", varnames(1) encoding(ISO-8859-1) clear
gen left_CEE=per4123+per4124+per4131+per4132
gen right_CEE=per4011+per4012+per4013
gen left_right_CEE=right_CEE-left_CEE


format %24s partyname

keep if countryname=="Albania"|countryname=="Bulgaria"|countryname=="Croatia"| ///
countryname=="Czech Republic"|countryname=="Estonia"|countryname=="Hungary"| ///
countryname=="Latvia"|countryname=="Lithuania"|countryname=="Macedonia"| countryname=="Poland"| ///
countryname=="Romania"|countryname=="Slovakia"|countryname=="Slovenia"

**create average ideology per country/election
*first create party weights based on their seats in parliament* their ideology

gen weighted_left=absseat/totseats*left_CEE
gen weighted_right=absseat/totseats*right_CEE
gen weighted_left_right=absseat/totseats*left_right_CEE

*create election average
by countryname date, sort: egen election_left=sum(weighted_left)
by countryname date, sort: egen election_right=sum(weighted_right)
by countryname date, sort: egen election_left_right=sum(weighted_left_right)


drop if countryname==countryname[_n-1]&date==date[_n-1]

drop if date>200500

keep countryname edate election_*

gen election_left2=election_left
gen election_right2=election_right
gen election_left_right2=election_left_right

split edate, p("/")

gen day=real(edate1)
gen month=real(edate2)
gen year=real(edate3)
gen year2=year

 expand 5
sort countryname year2
by countryname year, sort: replace year2=year2[_n-1]+1 if year2[_n-1]!=.

by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]
by countryname, sort: keep if year2<year2[_n+1]

gen date_formated=date(edate, "DMY")

**replace values with averages for the years where there are elections
*first create beginning and end of an year
gen firstday=1
gen lastday=31
gen firstmonth=1
gen lastmonth=12

gen begin_year=mdy(firstmonth, firstday, year2)
gen end_year=mdy(lastmonth, lastday, year2)

**compute the days between the beginning and end of year and the date of elections
gen firstpart=date_formated-begin_year if year!=year[_n-1]
gen secondpart=end_year-date_formated if year!=year[_n-1]

gen total_days=firstpart+second

*gen proportion of the year to each election
gen first_prop=firstpart/total_days
gen second_prop=secondpart/total_days


by countryname, sort: replace election_left2=second_prop*election_left+first_prop*election_left[_n-1] ///
if year!=year[_n-1]
by countryname, sort: replace election_right2=second_prop*election_right+first_prop*election_right[_n-1] ///
if year!=year[_n-1]
by countryname, sort: replace election_left_right2=second_prop*election_left_right+first_prop*election_left_right[_n-1] ///
if year!=year[_n-1]

keep countryname year2 election_left2 election_right2 election_left_right2

*create yearly averages

drop if year>2004
by year, sort: egen avg_left_CEE=mean(election_left2)
by year, sort: egen avg_right_CEE=mean(election_right2)
by year, sort: egen avg_left__right_CEE=mean(election_left_right2)


drop if year==year[_n-1]

keep year avg*

save "figures/avg_issues_CEE_figA1.dta", replace
